#include "CTexture.h"

#pragma region Constructor & Desc

CTexture::CTexture(void)
{
}

CTexture::~CTexture(void)
{
	if(m_texture != NULL)
	{
		m_texture->Release();
	}

	if(m_sprite != NULL)
	{
		m_sprite->Release();
	}
}

#pragma endregion 

#pragma region Methods

void CTexture::LoadTexture(LPDIRECT3DDEVICE9 d3ddev, LPD3DXSPRITE _dxSprite, char* path, D3DCOLOR transColor)
{
	m_sprite = _dxSprite;
	D3DXIMAGE_INFO imageInfo;
	HRESULT result = D3DXGetImageInfoFromFile(path,&imageInfo);
	if(result != D3D_OK)
	{
		return;
	}
	m_iWidth = imageInfo.Width;
	m_iHeight = imageInfo.Height;

	D3DXCreateTextureFromFileEx(d3ddev,
		path,
		imageInfo.Width,
		imageInfo.Height,
		1,
		D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		transColor,
		&imageInfo,
		NULL,
		&m_texture);

	if (result != D3D_OK)
	{
		return;
	}
}

void CTexture::Draw(int x, int y)
{
	D3DXMATRIX* mTransform = new D3DXMATRIX();
	D3DXMatrixIdentity(mTransform);

	m_sprite->SetTransform(mTransform);
	m_sprite->Draw(m_texture,
		NULL,
		NULL,
		&D3DXVECTOR3(0.0f,0.0f,0.0f), // cho nay hinh nhu d3dxvector3((float)x,(float)y,0.0f) day ma vi tri minh mun in texture ra 
		D3DCOLOR_XRGB(255,255,255));

	delete mTransform;
	mTransform = 0;
}

void CTexture::Draw(int x, int y, float xScaleRotation, float yScaleRotation,D3DXVECTOR2 vRotateCenter, float angle, D3DCOLOR color, RECT* rSrc, float deep)
{
	D3DXMATRIX* mTransform = new D3DXMATRIX();
	D3DXMatrixTransformation2D(mTransform,
		NULL,
		0,
		&D3DXVECTOR2(xScaleRotation,yScaleRotation),
		&vRotateCenter,
		angle,
		&D3DXVECTOR2((float)x,(float)y)); // cho nay la D3DXVECTOR2(0,0));

	m_sprite->SetTransform(mTransform);

	m_sprite->Draw(m_texture,rSrc,NULL,&D3DXVECTOR3(0,0,deep),color);  // cho nay la D3DXVECTOR3((float)x,(float)y,deep); vi tri minh mun in texture ra

	delete mTransform;
	mTransform = 0;
}

#pragma endregion 
